/*
* $Id: ChangeFamilyView.cpp 68 2010-03-18 19:49:39Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangeFamilyView.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/ComboBoxItem.h"
#include "Settings/Journal/FamilyViewSettings.h"
#include "Settings/SettingControlIds.h"
#include "Settings/DinIntDefined.h"
#include "Database/Tables/FamiliesTable.h"
#include "Database/Operation/ChangeFamilyOperation.h"

#define TITLE_ID			1
#define NAME_FAMILY_ID		2
#define FAMILY_TYPE_ID		3
#define NOTE_FAMILY_ID		4
#define USER_FIELD_ID		5

CChangeFamilyView::CChangeFamilyView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CChangeFamilyView::~CChangeFamilyView(){}

void CChangeFamilyView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(IApplication::GetApp()->GetLanguageManager()->GetString(CHANGE_FAMILY_ID), TITLE_ID));

	std::tstring strName;
	std::tstring strNote;
	int nIdRecord = 0, nType = 0, nIdHousing = 0;
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CFamiliesTable families;
	families.m_content.s_nId = m_nIdParameter;
	IResult* pResult = families.GetValue(pDb);
	if(pResult->GetSizeResult()){
		strName = ((CFamiliesTable::content*)(*pResult)[0])->s_strName;
		strNote = ((CFamiliesTable::content*)(*pResult)[0])->s_strNote;
		nIdRecord = ((CFamiliesTable::content*)(*pResult)[0])->s_nIdRecord;
		m_nIdPoint = ((CFamiliesTable::content*)(*pResult)[0])->s_nIdPoint;
		nType = ((CFamiliesTable::content*)(*pResult)[0])->s_type;
	}
	pResult->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_NAME_ID), NAME_FAMILY_ID, editItem::TEXT, strName));
	CFamilyViewSettings familyStn;
	CComboBoxItem* pItem = new CComboBoxItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_TYPE_ID), FAMILY_TYPE_ID);
	AddTypesFamily(pItem);
	pItem->SetValue(nType);
	m_editorDrawer.AddItem(pItem);
	if(familyStn.GetCurrentValue(CHECK_TYPE_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	CEditItem *pEditItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_FAMILY_ID, editItem::TEXT, strNote);
	m_editorDrawer.AddItem(pEditItem);
	if(familyStn.GetCurrentValue(CHECK_NOTE_ID) != CHECK_BOX_STATE_1)
		pEditItem->Hide(true);
	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(CViewFieldValuesHelper::POINTS, USER_FIELD_ID, nIdRecord);
}

void CChangeFamilyView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_CHANGE_FAMILY;
}
bool CChangeFamilyView::IsViewState(State state){
	return state.m_type == JournalEnum::CHANGE_FAMILY;
}

BEGIN_MESSAGE_MAP(CChangeFamilyView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSaveFamily)
END_MESSAGE_MAP()

void CChangeFamilyView::OnSaveFamily(){
	CChangeFamilyOperation operation;
	operation.m_content.s_nId = m_nIdParameter;
	operation.m_content.s_nIdPoint = m_nIdPoint;
	operation.m_content.s_strName = m_editorDrawer.GetValueItem(NAME_FAMILY_ID);
	operation.m_content.s_strNote = m_editorDrawer.GetValueItem(NOTE_FAMILY_ID);
	operation.m_content.s_nType = _ttoi(m_editorDrawer.GetValueItem(FAMILY_TYPE_ID).c_str());
	for(int i = 1; i <= m_nUserFields; i++)
		operation.m_content.s_aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	operation.UpdateValue(pDb);
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	__super::OnCancel();
}

void CChangeFamilyView::AddTypesFamily(CComboBoxItem* pItem){
	pItem->AddValueItem(CFamiliesTable::content::DEFAULT, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_DEFAULT_ID));
	pItem->AddValueItem(CFamiliesTable::content::PEDIGREE, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_PEDIGREE_ID));
	pItem->AddValueItem(CFamiliesTable::content::BREEDING, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_BREEDING_ID));
	pItem->AddValueItem(CFamiliesTable::content::FATHER, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_FATHER_ID));
	pItem->AddValueItem(CFamiliesTable::content::AMADOU, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_AMADOU_ID));
	pItem->AddValueItem(CFamiliesTable::content::BARREN, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_BARREN_ID));
	pItem->AddValueItem(CFamiliesTable::content::QUEEN_CELL, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_QUEEN_CELL_ID));
	pItem->AddValueItem(CFamiliesTable::content::QUEEN_CELL_SMALL, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_QUEEN_CELL_SMALL_ID));
	pItem->AddValueItem(CFamiliesTable::content::INDIVIDUAL_PROVINE, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_INDIVIDUAL_PROVINE_ID));
	pItem->AddValueItem(CFamiliesTable::content::COMBINED_PROVINE, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_COMBINED_PROVINE_ID));
	pItem->AddValueItem(CFamiliesTable::content::PACKET, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_PACKET_ID));
	pItem->AddValueItem(CFamiliesTable::content::NON_QUEEN, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_NON_QUEEN_ID));
	pItem->AddValueItem(CFamiliesTable::content::SWARM, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_SWARM_ID));
	pItem->AddValueItem(CFamiliesTable::content::USER, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_USER_ID));
}
